# 业务层View 实现记账和查看
import orm_amount
import time
def init_account():
    path=r'account.json'
    data_init_list = [{"date": "2021/8/21 09:00:00", "item": "工资", "classification": "收入", "amount": 20000},
            {"date": "2021/8/21 09:00:00", "item": "聚餐", "classification": "支出", "amount": -200},]
    orm_amount.write_data(data_init_list,path)
    print(orm_amount.read_data(path))

def add_account():
    path=r'account.json'

    date = time.strftime('%Y/%m/%d %H:%M:%S')
    item = input('请输入支出/收入项目：')
    classification = input('请输入支出/收入分类：')
    amount = int(input('请输入收入/支出金额：'))
    new_account_data = {"date": date, "item": item, "classification": classification, "amount": amount}
    #写入账单
    account_data_list = orm_amount.read_data(path)
    account_data_list.append(new_account_data)
    orm_amount.write_data(account_data_list,path)

    newest_account_data=orm_amount.read_data(path)#最新的记账本
    print(newest_account_data)#打印 记账本

def show_account():
    t_income=[]#收入列表
    t_expenditure=[]#支出列表

    path=r'account.json'
    account_data_list=orm_amount.read_data(path)#记账本数据

    i_frequency = 0  # 入账笔数
    e_frequency = 0  # 出账笔数
    for account in account_data_list:
        if account['amount']>0:
            t_income.append(account['amount'])
            i_frequency+=1
        elif account['amount']<0:
            t_expenditure.append(account['amount'])
            e_frequency+=1
    #总支出金额：
    total_expenditure=sum(t_expenditure)
    #总收入金额：
    total_income=sum(t_income)
    #结余金额
    total_amount=total_income+total_expenditure
    print(f'''您的总结余金额为{total_amount}元:
    \t共收入{i_frequency}笔，总收入金额为{total_income}元
    \t共支出{e_frequency}笔，总支出金额为{total_expenditure}元，''')


if __name__ == '__main__':
    #测试一下
    # init_account()
    # add_account()
    show_account()